<?php
    $this->layout('base.phtml', [
        'title' => _('Downloading your data'),
        'canonical' => url_full('exportation'),
        'back_options' => [
            'track' => false,
        ],
        'has_errors' => $error || $errors,
    ]);
?>

<div class="section section--small section--longbottom">
    <div class="section__title">
        <h1><?= _('Downloading your data') ?></h1>
    </div>

    <p class="section__intro">
        <?= _('You can download your data in order to migrate to a different tool.') ?>
    </p>

    <?= $this->include('alerts/_error.phtml', ['message' => $error]) ?>

    <?php if ($exportation && $exportation->status === 'ongoing'): ?>
        <p class="paragraph--featured" role="alert">
            <?= _('We’re creating your archive…') ?>
        </p>

        <div class="spinner"></div>

        <p class="paragraph--placeholder">
            <?= _('This might take several minutes, please wait.') ?>
        </p>

        <form
             method="get"
             action="<?= url('exportation') ?>"
             data-controller="autosubmit"
             data-autosubmit-mode-value="timeout"
             data-autosubmit-timeout-value="4000"
        >
        </form>
    <?php elseif ($exportation && $exportation->status === 'finished'): ?>
        <p class="paragraph--featured">
            <a class="anchor--action" href="<?= url('download exportation') ?>" data-turbo="false">
                <?= _('Download your archive') ?>
            </a>
        </p>

        <p class="paragraph--centered paragraph--secondary">
            <i>
                <?= _f('generated on %s', _date($exportation->created_at, 'dd MMMM Y')) ?>
            </i>
        </p>

        <form method="post" action="<?= url('create exportation') ?>">
            <input type="hidden" name="csrf" value="<?= csrf_token() ?>" />

            <div class="form__actions">
                <button type="submit">
                    <?= _('Generate a new data archive') ?>
                </button>
            </div>
        </form>
    <?php elseif ($exportation && $exportation->status === 'error'): ?>
        <?= $this->include('alerts/_error.phtml', ['message' => protect($exportation->error)]) ?>

        <form method="post" action="<?= url('create exportation') ?>">
            <input type="hidden" name="csrf" value="<?= csrf_token() ?>" />

            <div class="form__actions">
                <button type="submit" class="button--primary">
                    <?= _('Generate a new data archive') ?>
                </button>
            </div>
        </form>
    <?php else: ?>
        <form method="post" action="<?= url('create exportation') ?>">
            <input type="hidden" name="csrf" value="<?= csrf_token() ?>" />

            <div class="form__actions">
                <button type="submit" class="button--primary">
                    <?= _('Generate a data archive') ?>
                </button>
            </div>
        </form>
    <?php endif; ?>
</div>
